<template>
  <div>
    <div class="card" style="margin-bottom: 10px">
      <el-input placeholder="请输入患者姓名查询" style="width: 200px" v-model="data.patientName"></el-input>
      <el-button type="info" plain style="margin-left: 10px" @click="load()">查询</el-button>
      <el-button type="warning" plain style="margin-left: 10px" @click="reset">重置</el-button>
    </div>

    <div class="card">
      <div style="margin-bottom: 10px">
        <el-button type="primary" plain @click="handleAdd">新增</el-button>
        <el-button type="danger" plain @click="delBatch">批量删除</el-button>
      </div>
      <el-table :data="data.tableData" strip @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
<!--        <el-table-column prop="id" label="序号" width="70" align="center" sortable></el-table-column>-->
        <el-table-column prop="registrationId" label="挂号单ID" width="120"></el-table-column>
        <el-table-column prop="doctorId" label="医生ID"></el-table-column>
        <el-table-column prop="patientId" label="患者ID"></el-table-column>
        <el-table-column prop="hospitalized" label="是否住院"></el-table-column>
        <el-table-column prop="hospitalizationId" label="住院单ID" width="120"></el-table-column>
        <el-table-column prop="prescribedMedicine" label="是否开药"></el-table-column>
        <el-table-column prop="prescriptionId" label="药单ID" width="120"></el-table-column>
        <el-table-column prop="diagnosis" label="诊断信息" width="180"></el-table-column>
        <el-table-column prop="treatmentPlan" label="治疗方案" width="180"></el-table-column>
        <el-table-column prop="medicalAdvice" label="医嘱"  width="180"></el-table-column>
        <el-table-column prop="createdTime" label="叫号时间"  width="180"></el-table-column>
        <el-table-column prop="endTime" label="结束急诊时间"  width="180"></el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template v-slot="scope">
            <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            style="margin-top: 5px"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="data.pageNum"
            :page-sizes="[3, 5, 10, 15, 20]"
            :page-size="data.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="data.total">
        </el-pagination>
      </div>
    </div>

    <el-dialog title="新增或修改患者就诊信息" v-model="data.formVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
      <el-form :model="data.form" label-width="100px" style="padding-right: 50px" :rules="rules" ref="patientVisitForm">
        <el-form-item label="挂号单ID" prop="registrationId">
          <el-input v-model="data.form.registrationId" placeholder="挂号单ID"></el-input>
        </el-form-item>
        <el-form-item label="医生ID" prop="doctorId">
          <el-input v-model="data.form.doctorId" placeholder="医生ID"></el-input>
        </el-form-item>
        <el-form-item label="患者ID" prop="patientId">
          <el-input v-model="data.form.patientId" placeholder="患者ID"></el-input>
        </el-form-item>
        <el-form-item label="是否住院" prop="hospitalized">
          <el-select v-model="data.form.hospitalized" placeholder="请选择是否住院">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="住院单ID" prop="hospitalizationId">
          <el-input v-model="data.form.hospitalizationId" placeholder="住院单ID"></el-input>
        </el-form-item>
        <el-form-item label="是否开药" prop="prescribedMedicine">
          <el-select v-model="data.form.prescribedMedicine" placeholder="请选择是否开药">
            <el-option label="是" value="是"></el-option>
            <el-option label="否" value="否"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="药单ID" prop="prescriptionId">
          <el-input v-model="data.form.prescriptionId" placeholder="药单ID"></el-input>
        </el-form-item>
        <el-form-item label="诊断信息" prop="diagnosis">
          <el-input v-model="data.form.diagnosis" placeholder="诊断信息"></el-input>
        </el-form-item>
        <el-form-item label="治疗方案" prop="treatmentPlan">
          <el-input v-model="data.form.treatmentPlan" placeholder="治疗方案"></el-input>
        </el-form-item>
        <el-form-item label="医嘱" prop="medicalAdvice">
          <el-input v-model="data.form.medicalAdvice" placeholder="医嘱"></el-input>
        </el-form-item>
        <el-form-item label="叫号时间" prop="createdTime" >
          <el-date-picker type="datetime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                          v-model="data.form.createdTime" style="width: 100%" placeholder="叫号时间"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束就诊时间" prop="endTime" >
          <el-date-picker type="datetime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
                          v-model="data.form.endTime" style="width: 100%" placeholder="结束就诊时间"></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">保 存</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessageBox, ElMessage } from 'element-plus'
// 文件上传的接口地址
const uploadUrl = import.meta.env.VITE_BASE_URL + '/files/upload'

// 定义响应式数据
const data = reactive({
  tableData: [],  // 表格数据
  pageNum: 1,     // 当前页码
  pageSize: 10,   // 每页显示数量
  total: 0,       // 总记录数
  patientName: null,     // 搜索条件
  formVisible: false, // 表单显示状态
  form: {},       // 表单数据
  ids: [],        // 选中数据的ID
})
const patientVisitForm = ref()

// 分页查询
const load = () => {

  request.get('/PatientVisit/selectPage', {
    params: {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      patientName: data.patientName,
    }
  }).then(res => {
    if (res.code === '200') {
      data.tableData = res.data?.list
      data.total = res.data?.total
      console.log(res.data)
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 新增数据
const handleAdd = () => {
  data.form = {}
  data.formVisible = true
  console.log(data.formVisible)
}

// 编辑数据
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}

// 保存数据
const save = () => {
  const url = data.form.id ? '/PatientVisit/update' : '/PatientVisit/add'
  const method = data.form.id ? 'PUT' : 'POST'
  request({
    url,
    method,
    data: data.form,
  }).then(res => {
    if (res.code === '200') {
      load()
      ElMessage.success('保存成功')
      data.formVisible = false
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 删除单条数据
const del = (id) => {
  ElMessageBox.confirm('您确定删除吗？', '确认删除', { type: 'warning' }).then(() => {
    request.delete('/PatientVisit/delete/' + id).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(() => {})
}

// 批量删除
const delBatch = () => {
  if (!data.ids.length) {
    ElMessage.warning('请选择数据')
    return
  }
  ElMessageBox.confirm('您确定批量删除这些数据吗？', '确认删除', { type: 'warning' }).then(() => {
    request.delete('/PatientVisit/delete/batch', { data: data.ids }).then(res => {
      if (res.code === '200') {
        load()
        ElMessage.success('操作成功')
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(() => {})
}

// 处理选中的行
const handleSelectionChange = (rows) => {
  data.ids = rows.map(v => v.id)
}

// 重置筛选条件
const reset = () => {
  data.patientName = null
  load()
}

// 处理页码变化
const handleCurrentChange = (pageNum) => {
  data.pageNum = pageNum
  load(pageNum)
}
const handleSizeChange=(pageSize)=> {
  data.pageSize = pageSize
  load()
}

// 页面初始化加载数据
load()


// 校验规则
const rules = {
  patientId: [
    { required: true, message: '患者ID不能为空', trigger: 'blur' }
  ],
  doctorId: [
    { required: true, message: '医生ID不能为空', trigger: 'blur' }
  ],
  registrationId: [
    { required: true, message: '挂号单ID不能为空', trigger: 'blur' }
  ]
}

// 提交表单
const submitForm = () => {
  patientVisitForm.value.validate((valid) => {
    if (valid) {
      save()
    } else {
      ElMessage.error('请填写必须的参数！');
      return false;
    }
  });
}


</script>


<style scoped>

</style>